<template>
  <div class="app-container">
    <div class="view-title">菜单管理</div>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-permission="['menu:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      row-key="id"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column
        prop="name"
        label="菜单名称"
        :show-overflow-tooltip="true"
        width="180"
      />
      <el-table-column label="权限类型" width="140">
        <template slot-scope="{ row }">
          <span style="color: #409eff">{{
            row.type === 1
              ? "目录"
              : row.type === 2
              ? "菜单"
              : row.type === 3
              ? "按钮"
              : "/"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="route"
        label="路由地址"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="weight"
        label="权重"
        width="100"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="是否展示" width="160">
        <template slot-scope="{ row }">
          <span>{{
            row.is_show === 1 ? "是" : row.is_show === 2 ? "否" : "/"
          }}</span>
        </template>
      </el-table-column> -->

      <!-- <el-table-column prop="created_at" label="创建时间" /> -->
      <el-table-column prop="prop" label="操作" width="200" fixed="right">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-permission="['menu:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-permission="['menu:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :disabled="dialogType === 'view'"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="type">
              <el-select
                :disabled="dialogType === 'edit'"
                v-model="form.type"
                @change="typeChange"
                placeholder=""
              >
                <el-option label="目录" :value="1"></el-option>
                <el-option label="菜单" :value="2"></el-option>
                <el-option label="按钮" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.type !== 1">
            <el-form-item label="上级菜单" prop="pid">
              <el-select
                :disabled="dialogType === 'edit'"
                v-model="form.pid"
                placeholder=""
                v-if="form.type === 2"
              >
                <el-option
                  v-for="item in oneList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
              <el-select
                :disabled="dialogType === 'edit'"
                v-model="form.pid"
                placeholder=""
                v-if="form.type === 3"
              >
                <el-option
                  v-for="item in twoList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否展示" prop="is_show">
              <el-select v-model="form.is_show" placeholder="">
                <el-option label="显示" :value="1"></el-option>
                <el-option label="隐藏" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="权限名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入权限名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.type !== 3">
            <el-form-item label="菜单图标">
              <el-popover
                placement="bottom-start"
                width="460"
                trigger="click"
                @show="$refs['iconSelect'].reset()"
              >
                <IconSelect
                  ref="iconSelect"
                  @selected="selected"
                  :active-icon="form.icon"
                />
                <el-input
                  slot="reference"
                  v-model="form.icon"
                  placeholder="点击选择图标"
                  readonly
                >
                  <svg-icon
                    v-if="form.icon"
                    slot="prefix"
                    :icon-class="form.icon"
                    style="width: 25px"
                  />
                  <i
                    v-else
                    slot="prefix"
                    class="el-icon-search el-input__icon"
                  />
                </el-input>
              </el-popover>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="权重">
              <el-input-number
                style="width: 100%"
                v-model="form.weight"
                :min="0"
                placeholder="请输入权重"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="路由地址" prop="route">
              <el-input
                v-model="form.route"
                placeholder="请输入地址"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import IconSelect from "@/components/IconSelect";
import {
  getMenuList,
  deleteMenu,
  addMenu,
  getMenu,
  updateMenu,
  getAllPower,
} from "@/api/role";
export default {
  components: { IconSelect },
  data() {
    return {
      oneList: [],
      twoList: [],

      dialogType: "",
      //列表数据
      tableData: [],
      //遮罩层
      loading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        pid: null,
        name: null,
        icon: null,
        type: 1,
        is_show: null,
        weight: null,
        route: null,
      },
      // 表单校验
      rules: {
        pid: [{ required: true, message: "上级分类不能为空", trigger: "blur" }],
        name: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" },
        ],
        is_show: [
          { required: true, message: "是否展示不能为空", trigger: "blur" },
        ],
        route: [
          { required: true, message: "路由地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    typeChange() {
      this.form.pid = null;
    },
    /** 查询权限列表 */
    getList() {
      this.loading = true;
      getAllPower().then((response) => {
        this.tableData = response.data;
        this.loading = false;
        this.oneList = this.findNode(response.data, 1);
        this.twoList = this.findNode(response.data, 2);
      });
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.dialogType = "add";
      this.form = this.$options.data().form;
      this.open = true;
      this.title = "添加菜单";
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.dialogType = "view";
      this.form = this.$options.data().form;
      getMenu(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看菜单";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.dialogType = "edit";
      this.form = this.$options.data().form;
      getMenu(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜单";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("是否确认删除？")
        .then(() => {
          deleteMenu({ ids: row.id }).then((res) => {
            this.getList();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {});
    },

    // 选择图标
    selected(name) {
      this.form.icon = name;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.dialogType === "edit") {
            updateMenu(this.form).then((response) => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else if (this.dialogType === "add") {
            if (this.form.type === 1) {
              this.form.pid = 0;
            }
            addMenu(this.form).then((response) => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.open = false;
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    //过滤上级菜单
    findNode(data, type) {
      let newArr = [];
      function filterNode(node) {
        node.forEach((i) => {
          if (i.type === type) {
            newArr.push(i);
          }
          if (i.children && i.children.length > 0) {
            filterNode(i.children);
          }
        });
      }
      filterNode(data);
      return newArr;
    },
  },
};
</script>

<style lang="scss" scoped></style>
